#-*- coding: utf-8 -*-
import sys

if 'linux' in sys.platform:
    from Tkinter import *
elif 'win' in sys.platform:
    from tkinter import *


from lib.pipuii18n import _
from configuration import ConfigurationManager
from gui.proxymanager import ProxyManager
from gui.prefwindow import PreferencesWindow

cmanager = ConfigurationManager()

class MainWindow:
    """
        MainWindow class is a wrapper class to setup main
        window frame, its widgets and event handlers.
    """
    
    root = None
    master = None
    menu = None
    package_list = None
    
    current_package_name = ''
    current_log_list = []
    
    def __init__(self, master):
        self.master = master
        master.title(_(u"Python Package Installer"))

        self.setupMenu()
        self.setupWidgets()
        self.bindEvents()
        
    def setupWidgets(self):
        
        # configuring frame layout
        frame = Frame(self.master, height = 100)

        self.master.rowconfigure(0, weight = 1)
        self.master.columnconfigure(0, weight = 1)
        
        frame.grid(sticky = N+E+S+W)

        # configuring main window widgets grid layout
        frame.rowconfigure(0, weight = 1)
        frame.rowconfigure(1, weight = 2)
        frame.rowconfigure(2, weight = 2)
        frame.columnconfigure(0, weight = 6)
        frame.columnconfigure(1, weight = 2)
        
        # adding row and button for installing packages
        self.package_line = Entry(frame, textvariable = self.current_package_name, bd = 2)
        self.package_line.grid(column = 0, row = 0, padx = 10, pady = 10, sticky = W+E+N+S)
        
        self.btn_install = Button(frame, text = _(u'Install'), width = 10, command = self.commandInstall)
        self.btn_install.grid(column = 1, row = 0, padx = 10, pady = 10, sticky = W+E+N+S)
        
        # adding installed packages list widget
        self.package_list = Listbox(frame, height=30, width=100)
        self.package_list.grid(column = 0, row = 1, padx = 10, pady = 10, sticky = W+E+N+S, columnspan = 2)
        
        # adding log widget
        self.log_list = Listbox(frame, height = 5, width = 100, listvariable = self.current_log_list)
        self.log_list.grid(column = 0, row = 2, padx = 10, pady = 10, sticky = W+E+S+N, columnspan = 2)
        
    def bindEvents(self):
        self.master.bind('<Control-x>', self.commandProxySettingsS)
        self.master.bind('<Control-p>', self.commandPreferencesS)

        
    def setupMenu(self):
        # Creating main menu
        self.menu = Menu(self.master, relief = FLAT)
        self.master['menu'] = self.menu
        
        # Creating system menu
        self.system_menu = Menu(self.menu, relief = FLAT, tearoff = 0)
        self.menu.add_cascade(label = _(u"System"), menu = self.system_menu)
        self.system_menu.add_command(label = _(u"Quit"), command = self.commandQuit, accelerator = 'Ctrl+Q')
        
        # Creating options menu
        self.options_menu = Menu(self.menu, relief = FLAT, tearoff = 0)
        self.menu.add_cascade(label = _(u"Options"), menu = self.options_menu)
        self.options_menu.add_command(label = _(u"Preferences..."), command = self.commandPreferences, accelerator = 'Ctrl+P')
        self.options_menu.add_command(label = _(u"Proxy settings..."), command = self.commandProxySettings, accelerator = 'Ctrl+X')
        
        # Creating help menu
        self.help_menu = Menu(self.menu, relief = FLAT, tearoff = 0)
        self.menu.add_cascade(label = _(u"Help"), menu = self.help_menu)
        self.help_menu.add_command(label = _(u"Contents..."), command = self.commandHelpContents, accelerator = 'F1')
        self.help_menu.add_command(label = _(u"Index..."), command = self.commandHelpIndex)
        self.help_menu.add_separator()
        self.help_menu.add_command(label = _(u"About..."), command = self.commandAbout, accelerator = 'Ctrl+A')
        self.help_menu.add_separator()
        self.help_menu.add_command(label = _(u"Report about a problem"), command = self.commandAbout, accelerator = 'Ctrl+R')
    
    def commandPreferences(self):
        pref_window = PreferencesWindow(MainWindow.root)
        
    def commandProxySettings(self):
        proxy_manager = ProxyManager(MainWindow.root)
    
    commandProxySettingsS = staticmethod(commandProxySettings)
    commandPreferencesS = staticmethod(commandPreferences)
    
    def commandQuit(self):
        self.master.quit()

    def commandHelpContents(self):
        pass
        
    def commandAbout(self):
        pass
        
    def commandHelpIndex(self):
        pass

    def commandInstall(self):
        if not self.package_line.get():
            self.log_list.insert(END, _(u"You haven't entered package name to install"))
            return
        self.installPackage()

    def installPackage(self):
        self.log_list.insert(END, _(u"Searching package: %s ...") % self.package_line.get())

    def getInstallString(self):
        istring_tpl = 'pip install %s %s'
        package_name = self.package_line.get()
        if cmanager['proxy_login']:
            proxy = '--proxy=%s:%s@%s:%s' % (cmanager['proxy_login'], cmanager['proxy_password'], cmanager['proxy_host'], cmanager['proxy_host'])
        else:
            proxy = '--proxy=%s:%s' % (cmanager['proxy_host'], cmanager['proxy_port'])
        
